package com.example.notering;

import java.util.ArrayList;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import com.example.adapter.MyFragmentPagerAdapter;

public class MainActivity extends FragmentActivity{

	private ViewPager viewPager;
    private ArrayList<Fragment> fragments = new ArrayList<>();  
    private ImageView bottomLine;
    private Button tb1, tb2;
    private int currIndex = 0;
    private int offset;
	private int bmpW;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		InitImage();
        InitTextView();
        InitViewPager();
        
	}
//	public void Init() {
//		db = openOrCreateDatabase("my.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
//		DatabaseHelper.getInstance().createTable(db, "diary", CREATE_TABLE_DIARY);
//	}

	private void InitImage() {
		bottomLine = (ImageView) findViewById(R.id.cursor);
		
		DisplayMetrics  dm = new DisplayMetrics();     
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenWidth = dm.widthPixels;       
	     
		LayoutParams para;  
        para = bottomLine.getLayoutParams();
        para.width = screenWidth/2;  
        bottomLine.setLayoutParams(para);  
        
        bmpW = screenWidth/2;
        offset = (screenWidth/2 - bmpW)/2;
        
        
        Matrix matrix = new Matrix();  
        matrix.postTranslate(offset, 0);  
        bottomLine.setImageMatrix(matrix); 
	}

	private void InitViewPager() {
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		fragments = new ArrayList<Fragment>();

        Fragment fra1 = new DiaryFragment();  
        Fragment fra2 = WarnFragment.newInstance("添加闹钟");  

        fragments.add(fra1);
        fragments.add(fra2);

        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	private void InitTextView() {
		tb1 = (Button) findViewById(R.id.guide1);
		tb2 = (Button) findViewById(R.id.guide2);
        

		tb1.setOnClickListener(new MyOnClickListener(0));
		tb2.setOnClickListener(new MyOnClickListener(1));
	}	
	
	class MyOnClickListener implements View.OnClickListener {
		private int index=0;  
        
        public MyOnClickListener(int i) {  
            index =i;  
        }  
        @Override  
        public void onClick(View v) {
            viewPager.setCurrentItem(index);  
        }  
    }
    
    class MyOnPageChangeListener implements OnPageChangeListener {
    	private int one = offset *2 +bmpW;
        @Override
        public void onPageSelected(int arg0) {
        	Animation animation = new TranslateAnimation(currIndex*one,arg0*one,0,0);
            currIndex = arg0;  
            animation.setFillAfter(true); 
            animation.setDuration(200);
            bottomLine.startAnimation(animation);
        }

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}
    }
}
        
